import GlobalPool from "../../Common/GlobalPool";
import Loader from "../../Common/Loader";
import PlatformService from "../services/platformService";
import UserService from "../services/userService";
import UMe from "../system/OverallSystem ";
import { GameDataHelp } from "../system/gameDataHelp";

    
cc.macro.CLEANUP_IMAGE_CACHE = false;
cc.dynamicAtlasManager.enabled = true;
cc.dynamicAtlasManager.maxFrameSize = 512;

const { ccclass, property } = cc._decorator;

function beforeGameRun() {
    // 抗锯齿 要在cc.game.run前调用 webgl 才生效
    cc.macro.ENABLE_WEBGL_ANTIALIAS = true;
    // 是否开启多点触摸
    cc.macro.ENABLE_MULTI_TOUCH = false;
}
beforeGameRun();

@ccclass
export default class Start extends cc.Component {

    onLoad() {
        
        this.domainName(false);

        UMe.Initialize();

        const uiRoot: cc.Node = this.node.parent.getChildByName("UIRoot");
        cc.game.addPersistRootNode(uiRoot);
        UMe.Servers.uiSrv._setUIRoot(uiRoot);

        let tt = window['tt'];
        if(tt){
            tt.getSystemInfo({
                complete:((res)=>{
                    if(res){
                        PlatformService.platformName = res.appName;
                        console.warn("app名称",PlatformService.platformName);
                    }
                })
            })
        }

        let i = 750/1300
        let w = cc.winSize.width
        let h = cc.winSize.height
        // let bi = w>h? h/w : w/h
        let bi = w/h
        UserService.IPAD = true
        UserService.daren = false

        // // UserService.IPAD = true
        if(bi<i){
            UserService.IPAD = false
        }

        console.log("winSize:", cc.winSize);

        GameDataHelp.init()
    }

    start() {
        this.onReady();
    }

    private onReady() {

        this.initEngineConfig();

        this.printEngineConfig();

        this.goToLoading();
    }

    /** 初始化本框架配置 */
    private initEngineConfig() {
        //碰撞系统
        UMe.Managers.gameMgr.openPhysics(true, true);
        UMe.Managers.gameMgr.openCollision(true);
        //调试相关
        UMe.Managers.gameMgr.openPhysicsDebugDrawFlags(false);
        UMe.Managers.gameMgr.openCollisionDebugDraw(false);
        UMe.Managers.gameMgr.openDebugFPS(false);
    
    }

    /** 输出 cocos 配置 */
    private printEngineConfig() {
        console.group('cocos引擎配置');
        console.log('是否开启抗锯齿', cc.view.isAntiAliasEnabled(), cc.macro.ENABLE_WEBGL_ANTIALIAS);
        console.log('是否开启多点触摸', cc.macro.ENABLE_MULTI_TOUCH);
        console.log('是否显示左下角调试信息', cc.debug.isDisplayStats());
        console.log('是否开启瓦片地图的自动裁减功能', cc.macro.ENABLE_TILEDMAP_CULLING);
        console.log('单次批处理渲染的顶点数量', cc.macro.BATCH_VERTEX_COUNT);
        console.log('Canvas背景是否支持 alpha 通道', cc.macro.ENABLE_TRANSPARENT_CANVAS);
        console.log('引擎支持的图片格式', ...cc.macro.SUPPORT_TEXTURE_FORMATS);
        console.groupEnd();
    }

    goToLoading() {
        UMe.loadScene("loading");
    }

    domainName(bool: boolean) {
        let url: string = window.location.href;
        console.log(url);
        if (bool) {
            let host = window.location.host;
            if (host != "mini.ssflashgo.com") {
                cc.game.end();
            }
        }
    }

    
  

    // update (dt) {}
}
